package com.wesley.activemq;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * @author:wesley
 */
public class TopicSendMessage {

    private ApplicationContext ac;

    public TopicSendMessage() {
        ac = new ClassPathXmlApplicationContext("classpath*:spring-mvc.xml");
    }

    public void send(){
        JmsTemplate jmsTemplate = (JmsTemplate) ac.getBean("jmsTemplate");
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
            TextMessage msg = session.createTextMessage();
            msg.setText("这是生产者发出的数据");
            System.out.println("发送数据++++++++++++发送数据");
            return msg;
            }
        });
    }

    public void receive(){
        JmsTemplate jmsTemplate = (JmsTemplate) ac.getBean("jmsTemplate");
        Message msg = jmsTemplate.receive();
        TextMessage tm = (TextMessage)msg;
        System.out.println("非监听------------------非监听");
        System.out.println(msg);
    }

    public static void main(String[] args) {
        new TopicSendMessage().send();
    }

}
